/**************************************************************************
*   Copyright (C) 2009 Chernov Dmitry                                     *
*   diman4ik.chernov@gmail.com                                            *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "korners_render.h"
#include "build/korners.h"
#include <KSvgRenderer>
#include <KPixmapCache>
#include <QString>
#include <KGameTheme>
#include <KLocale>
#include <QPainter>



KornersRender::KornersRender()
{
    m_renderer = new KSvgRenderer();
    m_cache = new KPixmapCache( "korners-cache" );
    m_cache->setCacheLimit(5*1024);
    
    if( !m_cache->isEnabled() )
        kDebug() << "Cahce not enabled!!!";

    if ( !loadTheme( Prefs::theme() ) )
        kDebug()<< "Failed to load theme" << Prefs::theme();

}

KornersRender* KornersRender::self()
{
    static KornersRender instance;
    return &instance;
}

bool KornersRender::loadTheme(const QString& themeName)
{
    // variable saying whether to discard old cache upon successful new theme loading
    // we won't discard it if m_currentTheme is empty meaning that
    // this is the first time loadTheme() is called
    // (i.e. during startup) as we want to pick the cache from disc
    bool discardCache = true;

    if( !m_currentTheme.isEmpty() && m_currentTheme == themeName )
    {
        kDebug() << "Notice: not loading the same theme";
        return true; // this is not an error
    }

    KGameTheme theme;

    if ( !theme.load( themeName ))
    {
        kDebug() << "Failed to load theme" << themeName;
        kDebug() << "Trying to load default";
        if(!theme.loadDefault())
        {
            kDebug() << "Can't load default !!!";
            return false;
        }
    }

    m_currentTheme = themeName;

    bool res = m_renderer->load( theme.graphics() );
    if ( !res )
    {
        kDebug() << "Failed to load svg file !!!";
        return false;
    }

    if(discardCache)
    {
        kDebug() << "discarding cache";
        m_cache->discard();
    }

    return true;
}

QPixmap KornersRender::pixmapFromCache(const QString& svgName, const QSize& customSize) const
{
    if(m_cellSize == 0)
        return QPixmap();

    QPixmap pix;
    QSize sz = customSize.isValid() ? customSize : QSize( m_cellSize,m_cellSize );

    QString cacheName = svgName + QString("_%1x%2").arg( sz.width() ).arg( sz.height() );
    if( !m_cache->find(cacheName, pix) )
    {
        pix = QPixmap( sz );
        pix.fill( Qt::transparent );
        QPainter p( &pix );
        m_renderer->render( &p, svgName );
        p.end();
        m_cache->insert( cacheName, pix );
    }
    return pix;
}

QPixmap KornersRender::backgroundPixmap( const QSize& size ) const
{
    return pixmapFromCache( "background", size );
}

QPixmap KornersRender::backgroundTilePixmap() const
{
    return pixmapFromCache( "field_cell" );
}

QPixmap KornersRender::backgroundBorderPixmap( const QSize& size ) const
{
    return pixmapFromCache( "border", size );
}

QPixmap KornersRender::playerCapPixmap( ) const
{
    return pixmapFromCache( "player_cap" );
}

QPixmap KornersRender::opponentCapPixmap( ) const
{
    return pixmapFromCache( "opponent_cap" );
}

QPixmap KornersRender::moveHighlightPixmap() const
{
    return pixmapFromCache( "move_highlight" );
}

void KornersRender::setCellSize(int cellSize)
{
    if ( m_cellSize == cellSize )
        return;

    m_cellSize = cellSize;
}

bool KornersRender::hasBorderElement() const
{
    return m_renderer->elementExists( "border" );
}
